﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ReporterControlLibrary.Models;
using Reporter.Controls;
using Reporter.Data;
using System.Collections.Specialized;
using Reporter.UIHelpers;
using System.Collections.ObjectModel;
using System.ComponentModel;
using ReporterEngine;
using ReporterSystem;
using System.Reflection;

namespace Reporter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IShadeableWindow
    {
        ApplicationManager _manager;

        public MainWindow()
        {
            this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);

            ReportManager.Provider = new MyDocumentsProvider();

            AppData.Projects.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Projects_CollectionChanged);
        }

        void Projects_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                ProjectsTab.SelectedItem = e.NewItems[0];
            }
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            AppData.LoadData();

            CollectionViewSource cvs = this.TryFindResource("AppModelDataSource") as CollectionViewSource;
            if (cvs != null)
            {
                cvs.Source = AppData.Projects;
            }

            CollectionViewSource icvs = this.TryFindResource("InstanceNodesModelDataSource") as CollectionViewSource;
            if (icvs != null)
            {
                icvs.Source = AppData.InstanceNodes;
            }

            _manager = this.TryFindResource("ApplicationManager") as ApplicationManager;
            if (_manager != null)
            {
                _manager.AppWindow = this;
                _manager.AppWindowLayoutRoot = this.LayoutRoot;              
            }


            var version = Assembly.GetExecutingAssembly().GetName().Version;
            var _Version = "1" + "." + version.Major + "." + version.Minor + "." + version.Build + "." + version.Revision;

            this.Title += " version: " + _Version;
        }



        public void ActivateWindowShade()
        {
            this.dialogPopupBackground.Visibility = Visibility.Visible;
        }

        public void DeactivateWindowShade()
        {
            this.dialogPopupBackground.Visibility = Visibility.Collapsed;
        }
    }
 
}